#!/usr/bin/env python
import os
import subprocess
import time

def start_mes():
    print("正在启动MES服务...")
    
    # 设置环境变量
    os.environ['PATH'] = '/home/ubuntu/miniconda3/bin:' + os.environ.get('PATH', '')
    
    # 切换到项目目录
    os.chdir('/home/ubuntu/zlymes/mesmaster')
    print(f"当前目录: {os.getcwd()}")
    
    # 启动命令
    commands = [
        'source ~/miniconda3/etc/profile.d/conda.sh && conda activate mes && nohup python manage.py runserver 0.0.0.0:8004 --settings=settings_mes_basic > /tmp/mes_8004.log 2>&1 &',
        'source ~/miniconda3/etc/profile.d/conda.sh && conda activate mes && nohup python manage.py runserver 0.0.0.0:8005 --settings=settings_full_mes > /tmp/mes_8005.log 2>&1 &',
        'source ~/miniconda3/etc/profile.d/conda.sh && conda activate mes && nohup python manage.py runserver 0.0.0.0:8006 --settings=settings_django_with_mes > /tmp/mes_8006.log 2>&1 &'
    ]
    
    # 执行启动命令
    for i, cmd in enumerate(commands, 1):
        print(f"启动服务 {i}...")
        os.system(cmd)
        time.sleep(3)
    
    # 等待服务启动
    print("等待服务启动...")
    time.sleep(10)
    
    # 检查服务状态
    print("\n=== 检查服务状态 ===")
    for port in [8004, 8005, 8006]:
        result = os.system(f"lsof -t -i:{port} > /dev/null 2>&1")
        if result == 0:
            print(f"✓ 端口 {port} 服务正在运行")
        else:
            print(f"✗ 端口 {port} 服务未运行")
    
    print("\n=== 服务访问地址 ===")
    print("基础MES服务: http://175.27.138.86:8004/")
    print("基础MES管理后台: http://175.27.138.86:8004/admin/")
    print("完整MES服务: http://175.27.138.86:8005/")
    print("完整MES管理后台: http://175.27.138.86:8005/admin/")
    print("Django管理服务: http://175.27.138.86:8006/admin/")
    print("\n管理员账户: admin / admin123")
    print("\n服务启动完成！")

if __name__ == "__main__":
    start_mes() 